//
//  FQFileLogManager.m
//  FQLog
//
//  Created by ffq on 2023/3/13.
//

#import "FQFileLogManager.h"

@implementation FQFileLogManager

#pragma mark - override
// 日志名称规则
- (NSString *)newLogFileName {
    NSString *timeStamp = [self getTimestamp];
    return [timeStamp stringByAppendingString:@".log"];
}

// 是否是日志文件
- (BOOL)isLogFile:(NSString *)fileName {
    bool hasSuffix = [fileName hasSuffix:@".log"];
    return hasSuffix;
}

// log文件路径
- (NSString *)defaultLogsDirectory {

#if TARGET_OS_IPHONE
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *baseDir = paths.firstObject;
    NSString *logsDirectory = [baseDir stringByAppendingPathComponent:@"FQLog/Logs"];
#else
    NSString *appName = [[NSProcessInfo processInfo] processName];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? paths[0] : NSTemporaryDirectory();
    NSString *logsDirectory = [[basePath stringByAppendingPathComponent:@"FQLog/Logs"] stringByAppendingPathComponent:appName];
#endif

    return logsDirectory;
}

#pragma mark - private
- (NSString *)getTimestamp {
    static dispatch_once_t onceToken;
    static NSDateFormatter *dateFormatter;
    dispatch_once(&onceToken, ^{
        dateFormatter = [NSDateFormatter new];
        [dateFormatter setDateFormat:@"YYYY.MM.dd-HH:mm.ss"];
    });
    return [dateFormatter stringFromDate:NSDate.date];
}

@end
